package com.example.webchar.webchar.service.serverimpl;

import com.example.webchar.webchar.novelenum.NovelStartEnum;
import com.example.webchar.webchar.mapper.slave.NovelSlave;
import com.example.webchar.webchar.pojo.Novel;
import com.example.webchar.webchar.service.NovelService;
import com.example.webchar.webchar.utils.NovelTableName;
import com.example.webchar.webchar.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * @program: webchars
 * @description
 * @author: 毛叶贵子
 * @create: 2021-08-27 21:42
 **/
@Component
@Slf4j
public class NovelServImpl implements NovelService {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private NovelSlave novelSlave;

    private final static String NOVEL_CHAPTER_NOVEL_TYPE = "type_";
    private final static String NOVEL_INDEX = "index";

    @Override
    public List<Novel> fuzzyQueryNovel(String bookName) {
        return novelSlave.fuzzyQueryNovel(bookName + "%");
    }

    @Override
    public List<Novel> queryNovelType(String novelType) {
        Integer type;
        if (NovelTableName.FAN_TASY.equals(novelType)) {
            type = NovelStartEnum.FANTASY.ordinal();
            //判断是否为玄幻小说
        } else if (NovelTableName.FIXTHE_TRUTH.equals(novelType)) {
            type = NovelStartEnum.FIXTHETRUTH.ordinal();
            //修真小说
        } else if (NovelTableName.METR_OPOLIS.equals(novelType)) {
            type = NovelStartEnum.METROPOLIS.ordinal();
            //都市小说
        } else if (NovelTableName.THROUGH.equals(novelType)) {
            type = NovelStartEnum.THROUGH.ordinal();
            //网游小说
        } else if (NovelTableName.ONLINE.equals(novelType)) {
            type = NovelStartEnum.ONLINE.ordinal();
            //网游
        } else if (NovelTableName.SCIENECFICTION.equals(novelType)) {
            type = NovelStartEnum.SCIENECFICTION.ordinal();
            //科幻
        } else {
            type = NovelStartEnum.OTHER.ordinal();
            //其他小说
        }
        if (!redisUtils.hasKey(NOVEL_CHAPTER_NOVEL_TYPE + type)) {
            redisUtils.set(NOVEL_CHAPTER_NOVEL_TYPE + type, novelSlave.queryNovelType(type), 60 * 60 * 6);
            log.info("走数据库");
            return novelSlave.queryNovelType(type);
        } else {
            log.info("走缓存");
            return (List<Novel>) redisUtils.get(NOVEL_CHAPTER_NOVEL_TYPE + type);

        }

    }

    @Override
    public List<List<Novel>> queryNovelIndex() {
        if (!redisUtils.hasKey(NOVEL_INDEX)) {
            List<List<Novel>> novels = new ArrayList<>(8);
            novels.add(novelSlave.quereyNovelByCreateTimeDesc());
            novels.add(novelSlave.queryNovelByUpdateTimeDesc());
            novels.add(novelSlave.queryNovelTypeLimit(0));
            novels.add(novelSlave.queryNovelTypeLimit(1));
            novels.add(novelSlave.queryNovelTypeLimit(2));
            redisUtils.set(NOVEL_INDEX, novels, 60 * 60 * 6);
            //设置过期时间为6小时
            log.info("走数据库");
            return novels;
        } else {
            List<List<Novel>> novels = (List<List<Novel>>) redisUtils.get(NOVEL_INDEX);
            //获取redis信息
            log.info("走缓存");
            return novels;
        }

    }

    @Override
    public List<Novel> queryNovelByName(String name) {
        List<Novel> novels = novelSlave.queryNovelByName(name);
        return novels;
    }
}

